//
//  AddRenYuanGuanLiVC.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/9.
//

#import "AddRenYuanGuanLiVC.h"
#import "AddSuccessWithGuanLiVC.h"
#import "RenYuanDetailFooterView.h"
#import "JueSeModel.h"
@interface AddRenYuanGuanLiVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView               *tableView;
@property (nonatomic, strong) NSMutableArray            *dataAry;
@property (nonatomic, strong) NSString                  *nickName;
@property (nonatomic, strong) NSString                  *roleName;
@property (nonatomic, strong) NSString                  *roleId;
@property (nonatomic, strong) NSString                  *realName;//真名
@property (nonatomic, strong) NSString                  *inviteCode;
@property (nonatomic, strong) NSString                  *userPhone;
@property (nonatomic, strong) CommonChoseTypeRoleView   *choseTypeView;
@property (nonatomic, strong) NSMutableArray            *jueSeArray;



@end

@implementation AddRenYuanGuanLiVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
    [self layoutCustomNav:self.view title:@"人员添加"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nickName = @"";
    self.roleName = @"";
    self.userPhone = @"";
    self.realName = @"";
    [self getJueSeList];
     self.dataAry = [NSMutableArray arrayWithArray:@[@"别名",@"角色",@"真实姓名",@"手机号"]];
     self.jueSeArray = [NSMutableArray array];
    [self createTableView];
}

- (void)createTableView{
    self.tableView = [[UITableView  alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, ScreenWidth, B_HEIGHT - LL_StatusBarAndNavigationBarHeight - LL_TabbarSafeBottomMargin)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:[CommonTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[CommonTableViewCell reuseIdentifier]];
    [self.view addSubview:self.tableView];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataAry.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[CommonTableViewCell reuseIdentifier]];
    if (!cell) {
        cell=[[CommonTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CommonTableViewCell reuseIdentifier]];
    }
    cell.nameLb.text = self.dataAry[indexPath.row];
    cell.lineView.hidden = YES;
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    cell.indexPath = indexPath;
    [cell setCurrentValueBlock:^(NSString * _Nonnull contentString, NSIndexPath * _Nonnull indexPath) {
        if (indexPath.row == 0){
            self.nickName = contentString;
        }else if (indexPath.row== 2){
            self.realName = contentString;
        }else if (indexPath.row == 3){
            self.userPhone = contentString;
        }
    }];
    if (indexPath.row == 0) {
        cell.contentTF.userInteractionEnabled = YES;
        if ([NSString isNUllString:self.nickName]) {
            cell.contentTF.placeholder = @"请输入别名";
            cell.contentTF.text = @"";
        } else {
            cell.contentTF.text = self.nickName;
        }
    }else if (indexPath.row == 1){
        cell.contentTF.userInteractionEnabled = NO;
        if ([NSString isNUllString:self.roleName]) {
            cell.contentTF.placeholder = @"请选择角色";
            cell.contentTF.text = @"";
        } else {
            cell.contentTF.text = self.roleName;
        }
    }else if (indexPath.row == 2){
        cell.contentTF.userInteractionEnabled = YES;
        if ([NSString isNUllString:self.realName]) {
            cell.contentTF.placeholder = @"请输入真实姓名";
            cell.contentTF.text = @"";
        } else {
            cell.contentTF.text = self.realName;
        }
        
    } else {
        cell.contentTF.userInteractionEnabled=YES;
        if ([NSString isNUllString:self.userPhone]) {
            cell.contentTF.placeholder = @"请输入手机号";
            cell.contentTF.text = @"";
        } else {
            cell.contentTF.text = self.userPhone;
        }
     }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [CommonTableViewCell cellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1){
        BD_WEAKSELF
        [weakSelf.view endEditing:YES];
        UIWindow *currentWindow = [BaseViewController getCurrentWindow];
        if(weakSelf.choseTypeView){
            [weakSelf.choseTypeView removeFromSuperview];
        }
        weakSelf.choseTypeView = [CommonChoseTypeRoleView shareInstance];
        weakSelf.choseTypeView.frame = CGRectMake(0, 0, ScreenWidth, B_HEIGHT);
        [weakSelf.choseTypeView DataSourceToShowAlert:weakSelf.jueSeArray withSelectTitle:weakSelf.roleName withType:@"1" withTitle:@"请选择角色"];
        [currentWindow addSubview:weakSelf.choseTypeView];
        [currentWindow bringSubviewToFront:weakSelf.choseTypeView];
        [weakSelf.choseTypeView setToSendValue:^(JueSeModel * _Nonnull model) {
            weakSelf.roleName = model.roleName;
            weakSelf.roleId = model.roleId;
            [weakSelf.tableView reloadData];
            [weakSelf.choseTypeView removeFromSuperview];
        }];
        [weakSelf.choseTypeView.tableView reloadData];
    }
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 150)];

    RenYuanDetailFooterView *detailView = [RenYuanDetailFooterView shareInstance];
    detailView.frame = CGRectMake(0, 0, ScreenWidth, 150);
    [footerView addSubview:detailView];
    if (self.inviteCode.length == 0) {
        detailView.yaoQingLb.hidden = YES;
        detailView.fuZhiButton.hidden = YES;
        detailView.statusButton.hidden = YES;
    } else {
         detailView.yaoQingLb.hidden = NO;
         detailView.fuZhiButton.hidden = NO;
         detailView.statusButton.hidden = NO;
         detailView.yaoQingLb.text = [NSString stringWithFormat:@"邀请码:%@",self.inviteCode];
     }
    [detailView.fuZhiButton whenTapped:^{
        UIPasteboard *copy = [UIPasteboard generalPasteboard];
        [copy setString:self.inviteCode];
     if (copy == nil || [self.inviteCode isEqualToString:@""]){

     } else {
            showAlert(@"复制成功");
     }
    }];
    [detailView.activeButton whenTapped:^{
        [self commitInfo];
    }];
    return footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 150;
}

-(void)commitInfo{
    [self.view endEditing:YES];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    if (self.nickName.length == 0) {
        showAlert(@"请输入别名");
        return;
    }
    [params setObject:self.nickName forKey:@"nickName"];
    
    if (self.roleName.length== 0) {
        showAlert(@"请请选择角色");
        return;
    }
    [params setObject:self.roleName forKey:@"roleName"];
    [params setObject:self.roleId forKey:@"roleId"];
    
    if (self.realName.length == 0) {
        showAlert(@"请输入真实姓名");
        return;
    }
    [params setObject:self.realName forKey:@"realName"];
    [params setObject:self.userPhone forKey:@"userPhone"];
    if (self.userPhone.length != 11){
        showAlert(@"请输入正确的手机号");
        return;
    }
    
    
    [MSNetworkManager postRevertUrl:POST_PersonAddUser_URL params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            showAlert(@"人员添加成功");
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    

}

///获取角色列表
-(void)getJueSeList{
    WEAKSELF
    
    [MSNetworkManager postRevertUrl:POST_tJurisdiction params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            for (NSDictionary *dic in resonseObj[@"roleList"]) {
                JueSeModel *model = [[JueSeModel alloc] initWithDic:dic];
                if ([model.roleCode isEqualToString:@"enter_admin"]) {
                }else{
                    [weakSelf.jueSeArray addObject:model];
                }
            }
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    

}

@end
